package com.ylx.excel.config;

import lombok.extern.slf4j.Slf4j;
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;

/**
 * 线程池
 *
 * @author yanglixin
 */
@Slf4j
//@Configuration
//@EnableAsync
public class TaskPoolConfig {

    //@Bean("taskExecutor")
    public ThreadPoolTaskExecutor taskExecutor() {
        // Java 虚拟机可用的处理器数量
        int i = Runtime.getRuntime().availableProcessors();
        log.info("系统最大线程数: " + i);

        ThreadPoolTaskExecutor taskExecutor = new ThreadPoolTaskExecutor();
        // 设置核心线程池大小
        taskExecutor.setCorePoolSize(i);
        // 设置最大线程池大小
        taskExecutor.setMaxPoolSize(i);
        // 设置队列容量
        taskExecutor.setQueueCapacity(99999);
        // 设置线程存活时间（秒）
        taskExecutor.setKeepAliveSeconds(60);
        // 设置线程名称前缀
        taskExecutor.setThreadNamePrefix("taskExecutor--");
        // 设置在关闭时等待任务完成
        taskExecutor.setWaitForTasksToCompleteOnShutdown(true);
        // 设置等待任务完成的最大时间（秒）
        taskExecutor.setAwaitTerminationSeconds(60);

        return taskExecutor;
    }
}

